/**
 * 
 */
var zTree="";	//ztree用于处理前进后退上下章节
var curNode="";	//当前节点
var url="";	//当前节点的url
var dmId="";//当前DM
var projectId=""; //项目Id
var projectCode=""; //项目code
var colorVal="";//背景颜色
var fontSize="";//字体大小
var guolvFit="";//过滤条件 符合条件的适用性
var currentPos=0;	//历史记录当前位置
var historyArr=[]; //历史记录
var requesturl=""; //请求路径如http://localhost:8082/，用于openWrl()函数的url调用
var userName="";
var pdfName="";	//pdf路径

$(function() {
	adaptive();
	if($('#url').val().split("/")[2]){
		projectCode=$('#url').val().split("/")[2]
	}
	requesturl=$('#requesturl').val();
	adaptive();
	homeTabs();
	loadTree();
});
//ztree setting
var setting = {
		view: {
			showLine: false,
			showIcon: false,
			selectedMulti: false,
			dblClickExpand: false
		}
//	,async : {
//			enable : true,
//			url : getUrl
//		}
	,
		data: {
			simpleData: {
				enable: true
			}
		}/*,
		callback: {
			beforeClick: beforeClick,
			onClick : onClick,
			onNodeCreated: zTreeOnNodeCreated
		}*/
	};
/**
 * 加载树获取pm、dm列表，不在页面显示
 */
function loadTree() {
	dmId=$('#dmId').val();
	projectId=$('#projectId').val();
 		var param={};
		param.projectId=projectId;
		param.fitName=guolvFit;
		$.ajax({
			async : false,
			type : 'POST',
			dataType : "json",
			url :"/tree/initTree?type=ietm",//请求的action路径
			data:param,
			success : function(data) { //请求成功后处理函数。
			zTree=$.fn.zTree.init($("#treeDemo"), setting, data);
			curNode=zTree.getNodeByParam("id",dmId);
			if(null!=curNode){
			$('#dmTitle').html(curNode.name);
			$("#publiciframe").attr('src',curNode.url);
			recordHistory(curNode);
			}
		}
	});
}
/**
 * 点击下一章节
 */
function nextDm(){
	if(curNode.isLastNode){
		do{
			curNode=curNode.getParentNode();
		}while(curNode.isLastNode&&curNode.level!==0);
		if(curNode.level!==0){
			curNode=curNode.getNextNode();
		}
		while(curNode.isParent){
			curNode=curNode.children[0];
		}
	}
	else{
		curNode=curNode.getNextNode();
	}
	if(null!=curNode&&null!=curNode.url){
	url=curNode.url;
	dmId=curNode.id;
	$('#dmTitle').html(curNode.name);
	$("#publiciframe").attr('src',url);
	recordHistory(curNode);
	}
}
function textSearch(){
	var sea=$("#checkContent").val();
	if(sea&&''!==sea.trim()){
		$("#publiciframe").contents().find("body").textSearch(sea);
	//	document.getElementById('publiciframe').contentWindow.textSearch11();
	}

}

/**
 * 点击上一章节
 */

function preDm(){
	if(curNode.isFirstNode){
		do{
			curNode=curNode.getParentNode();
		}while(curNode.isFirstNode&&curNode.level!==0);
		if(curNode.level!==0){
			curNode=curNode.getPreNode();
		}
		while(curNode.isParent){
			var childNodes=curNode.children;
			curNode=childNodes[childNodes.length-1];
		}
	}
	else{
		curNode=curNode.getPreNode();
	}
	if(null!=curNode&&null!=curNode.url){
	url=curNode.url;
	dmId=curNode.id;
	$('#dmTitle').html(curNode.name);
	$("#publiciframe").attr('src',url);
	recordHistory(curNode);
	}
}
/**
 * 保存字体大小和背景颜色
 */
function saveFontAndColor(){
	 var thisEle = $('#publiciframe').contents().find("#article").css("font-size"); // 获取当前字体大小
	 fontSize = parseInt(thisEle);
     colorVal=$('#publiciframe').contents().find("body").css("background"); //背景颜色
     $.ajax({
    	 url:'/ietm/saveSession',
    	 data:{color:colorVal,font:fontSize,type:'2'},
    	 success:function (){
    		 //location.reload();
    		 $(".dm-moreaar").click();
    	 }
     });
}
/**
 * dm浏览页面如果返回,回到dm列表页面
 */
function contentBack(){
	var type=$("#type").val();
	if("list"===type){
		history.go(requesturl+'tree/getChildrenList?id='+$('#pmId').val());
	}else if("tag"===type){
		history.go(requesturl+'ietm/annotations.html');
	}else if("comment"===type){
		history.go(requesturl+'ietm/annotations.html');
	}else if("userhelp"===type){
		history.go(requesturl+'ietm/user-help.html');
	}
	else{
		history.go(-1);
	}
}
/**
 * 当前dm如果链接到其他dm，记录历史，实现前进后退功能
 */
function refDMparent(code){
	var nodes = zTree.getNodeByParam("dmCode",code);
	if(null!=nodes){
		$('#dmTitle').html(nodes.name);
		$("#publiciframe").attr('src',nodes.url);
		//点击过的历史记录DM链DM的前进后退
		recordHistory(nodes);
	}
	}
/**
 * 记录历史，实现前进后退功能
 */
function recordHistory(node){
	if (currentPos >= historyArr.length - 1){
        historyArr.push(node.id);
        currentPos = historyArr.length - 1;
    }else{
        historyArr.splice(currentPos + 1, 0, node.id);
        currentPos = currentPos + 1;
    }
}
/**
 * dm间相互链接跳转产生历史间的前进
 */
function goNext(){
	var lastPos = currentPos;
    if (0 == historyArr.length-1){
        currentPos = 0;
    }else{
        currentPos = (currentPos >= (historyArr.length-1) ? (historyArr.length-1) : (currentPos+1));
    }
    if (lastPos == currentPos){
    	return;
    }
    var currentNodeId = historyArr[currentPos];
    if (historyArr[lastPos] == currentNodeId){ // 如果一样，则继续向前寻找
    	goNext();
        return;
    }
    var node = zTree.getNodeByParam("id",currentNodeId);
    $('#dmTitle').html(node.name);
	$("#publiciframe").attr('src',node.url);
}
/**
 * dm间相互链接跳转产生历史间的后退
 */
function goPre(){
	var lastPos = currentPos;
    currentPos = (currentPos <= 0 ? 0 : (currentPos-1));
    if (lastPos == currentPos){
        return;
    }
    var currentNodeId = historyArr[currentPos];
    if (historyArr[lastPos] == currentNodeId){ // 如果一样，则继续向后寻找
    	goPre();
        return;
    }
    var node = zTree.getNodeByParam("id",currentNodeId);
    $('#dmTitle').html(node.name);
	$("#publiciframe").attr('src',node.url);
}
